<?php

namespace app\api\controller\v1\article;

use app\model\article\ArticleType;
use app\Request;

class ArticleController
{
    public function type()
    {
        $list = ArticleType::listAll('id,type_name') ;
        return app('json')->success(['list' => $list]);
    }
    public function index(Request $request)
    {
        list($type , $page) = $request->more([
            ['type_id' , 1] ,
            ['page' , 1] ,
        ] , true) ;
        $list = \app\model\article\Article::field('id,title,create_time,top_status,see_total,img_url')
            ->where('type_id' , $type)
            ->page($page , 10)->order('top_status asc,id desc')->select()->each(function ($data){

                 $data['new_status'] = 1 ;
                 return $data ;
            });
        return app('json')->success(['list' => $list]);
    }
    public function detail(Request $request)
    {
        list($id) = $request->more([
            ['id' , 1] ,
        ] , true) ;
        $info = \app\model\article\Article::getOne($id) ;

        $info->see_total = $info->see_total + 1;
        $info->save() ;

        return app('json')->success(['info' => [
            'title' => $info['title'] ,
            'see_total' => $info['see_total'] ,
            'content' => $info['content'] ,
            'create_time' => $info['create_time'] ,
        ]]);
    }
}